package com.imonsoft.common.base.aop;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.imonsoft.common.exception.SystemException;
import com.imonsoft.common.util.RequestUtil;

/**
 * 权限控制（切面类）
 */
public class AopAspectController {

	protected final Log logger = LogFactory.getLog(getClass());

	public void doBefore(JoinPoint jp) throws Exception {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		if (logger.isDebugEnabled()) {
			Map<String, String> params = RequestUtil.getParameterMap(request);
			String uri = request.getRequestURI().toLowerCase();
			logger.debug("[IP:" + RequestUtil.getClientAddress(request) + ",Port:" + RequestUtil.getRemotePort(request) + "] URI:" + uri + "\n 参数：" + params);
		}
	}

	public void doThrowing(JoinPoint jp, Throwable ex) {
		if (ex instanceof SystemException) {
			logger.debug(jp.getTarget().getClass().getName() + "." + jp.getSignature().getName() + "异常内容: " + ex.getMessage());
		} else {
			logger.error(jp.getTarget().getClass().getName() + "." + jp.getSignature().getName() + "异常内容: " + ex.getMessage());
		}
	}

}
